package com.ljq.cksp.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljq.cksp.dao.DocumentDao;
import com.ljq.cksp.dao.LawDao;
import com.ljq.cksp.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:ljq
 * @Date:2020/3/3
 */
@Service
public class JsonUtil {
    @Autowired
    DocumentDao documentDao;
    @Autowired
    LawDao lawDao;

    public static List<Document> json2Documents(File file){
        ObjectMapper objectMapper = new ObjectMapper();
        List<Document> documents = new ArrayList<>();
        try{
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String json;
            while((json = reader.readLine()) !=null){
                Document document = new Document();
                JsonNode jsonNode = objectMapper.readTree(json);
                document.setFact(jsonNode.get("fact").asText());
                JsonNode meta = jsonNode.get("meta");
                List<Integer> relevantArticles = new ArrayList<>();
                meta.get("relevant_articles").forEach(f ->relevantArticles.add(f.asInt()));
                document.setRelevantArticles(relevantArticles);
                List<String> accusation = new ArrayList<>();
                meta.get("accusation").forEach(f -> accusation.add(f.asText()));
                document.setAccusation(accusation);
                List<String> criminals = new ArrayList<>();
                meta.get("criminals").forEach(f -> criminals.add(f.asText()));
                document.setCriminals(criminals);
                JsonNode termOfImprisonment = meta.get("term_of_imprisonment");
                document.setDeathPenalty(termOfImprisonment.get("death_penalty").asBoolean());
                document.setImprisonment(termOfImprisonment.get("imprisonment").asInt());
                document.setLifeImprisonment(termOfImprisonment.get("life_imprisonment").asBoolean());
                documents.add(document);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return documents;
    }

    public static List<Law> json2Laws(File json){
        ObjectMapper objectMapper = new ObjectMapper();
        List<Law> laws = new ArrayList<>();
        try{
            JsonNode jsonNode = objectMapper.readTree(json).get("data");
            String title = jsonNode.get("title").asText()+"/";
            for(JsonNode pieceNode: jsonNode.get("mainBody")) {
                String title1 = title.concat(pieceNode.get("pieceNum").asText()+"/");
                for(JsonNode chapterNode: pieceNode.get("content")){
                    String title2 = title1.concat(chapterNode.get("title").asText()+"/");
                    for(JsonNode partNode: chapterNode.get("content")){
                        Law law = new Law();
                        law.setTitle(title2+partNode.get("title").asText());
                        law.setContent(partNode.get("text").asText());
                        laws.add(law);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return laws;
    }

    public static Book json2Book(File json){
        Book book = new Book();
        ObjectMapper objectMapper = new ObjectMapper();
        try{
            JsonNode jsonNode = objectMapper.readTree(json).get("data");
            book.setTitle(jsonNode.get("title").asText());
            book.setLabel(jsonNode.get("label").asText());
            List<Piece> mainBody = new ArrayList<>();
            for(JsonNode pieceNode: jsonNode.get("mainBody")) {
                Piece piece = new Piece();
                piece.setPieceNum(pieceNode.get("pieceNum").asText());
                List<Chapter> content = new ArrayList<>();
                for(JsonNode chapterNode: pieceNode.get("content")){
                    Chapter chapter = new Chapter();
                    chapter.setTitle(chapterNode.get("title").asText());
                    List<Part> chapterContent = new ArrayList<>();
                    for(JsonNode partNode: chapterNode.get("content")){
                        Part part = new Part();
                        part.setNums(partNode.get("index").asInt());
                        part.setTitle(partNode.get("title").asText());
                        part.setText(partNode.get("text").asText());
                        chapterContent.add(part);
                    }
                    chapter.setContent(chapterContent);
                    content.add(chapter);
                }
                piece.setContent(content);
                mainBody.add(piece);
            }
            book.setMainBody(mainBody);
        }catch (Exception e){
            e.printStackTrace();
        }
        return book;
    }

}
